// eslint-disable-next-line
/* eslint-disable */
// 对 axios.get() 和 axios.post()进行封装
import axios from 'axios'
import qs from 'qs'

/*
post 方式
axios({
	method: 'post',
	url: '/user/123456',
	data: {
		username: 'ken',
		password: '123456'
	}
})
axios.post('/user', {
    firstName: 'Fred',
    lastName: 'Flintstone'
})

get 方式
axios({
	method: 'get',
	url: '/user/123456',
})

// 可选地，上面的请求可以这样做
axios.get('/user', {
	params: {
	  ID: 12345
	}
})


//伪代码的编写
axios({
	method: 'post' || 'get',
	url: '/user/123456',
	data: {
		username: 'ken',
		password: '123456',
	} || params: {
		ID: '123456'
	}
})
*/
export default function axios_get_post(params){
	return new Promise((resolve, reject)=>{
		let opt = {
			method: params.method || 'get',
			url: params.url,
			headers: {
				'Content-Type':'application/x-www-form-urlencoded'
			},
		}
		if(params.method == 'post'){
			opt.data = params.data
		}else {
			// 
			opt.params = params
		}
		// 拦截处理
		axios.interceptors.request.use((req) => {
		    if (req.method === 'post') {
		    	// 转换成JSON格式
		     	req.data = qs.stringify(req.data);
		    }
		    return req;
		}, (error) => Promise.reject(error));

		axios(opt).then(res=>{
			resolve(res.data)
		}).catch(err=>{
			reject(err)
		})
	})
}
